package org.shoukaiseki.supersaiyan.controller;

import org.apache.commons.lang3.RandomUtils;
import org.shoukaiseki.common.random.RandomDate;
import org.shoukaiseki.common.utils.DateUtils;
import org.shoukaiseki.common.utils.NumberUtils;
import org.shoukaiseki.supersaiyan.common.core.controller.BaseController;
import org.shoukaiseki.supersaiyan.common.core.domain.base.AjaxResult;
import org.shoukaiseki.supersaiyan.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 *
 **/
@RestController
@RequestMapping("/anon/test")
public class TestController extends BaseController {

    @Autowired
    public RedisCache redisCache;

    /**
     * 用户授权角色
     */
    @GetMapping("/mockData")
    public AjaxResult mockData()
    {
        Long id=801010011L;
        int expireTime=60*24*2;
        for (int i = 0; i <2000*10000; i++) {
            int status = RandomUtils.nextInt(0, 3);
            String key =  "331003198"+(id+i);
            Date date = RandomDate.nextDate("2022-09-13 00:00:00", "2022-10-12 23:59:59");
            long l = date.getTime() * 10;
            long l1 = l + status;
            redisCache.setCacheObject(key,l1,expireTime, TimeUnit.MINUTES);
        }
        return success("生成随即健康码"+2000*10000+"条");
    }

    @GetMapping("/personCode")
    public AjaxResult personCode(String id){
        Long l = redisCache.getCacheObject(id);
        if(l==null){
            return AjaxResult.error("无核算记录");
        }
        long l1 = l / 10;
        Date date = new Date(l1);
        String s = DateUtils.formatToStr(date);
        long l2 = l % 10;
        String status="";
        if (NumberUtils.isEquals(l2,0L)) {
           status="绿码";
        }else if (NumberUtils.isEquals(l2,1L)) {
            status="黄码";
        }else if (NumberUtils.isEquals(l2,2L)) {
            status="红码";
        }
        return AjaxResult.successData(status+"\t"+"最后一次核算检测时间:"+s);
    }

    @GetMapping("/queryTotal")
    public AjaxResult queryTotal(){
        Object total = redisCache.dbSize();
        return AjaxResult.successData("总条数:"+total);
    }

}
